﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using insurance.application;
using System.Collections;

public partial class product_insurance_user_insurance_apply_accident : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((AfterLogin)this.Master).mainCategory = "ACCOUNTS";
        if (!IsPostBack)
        {
            this.btnSubmit.Enabled = false;
            this.tbi_firstName.Text = Session["fName"] as String;
            this.tbi_lastName.Text = Session["lName"] as String;
            this.tbi_nric.Text = Session["custNRIC"] as String;
            this.tbi_nationality.Text = Session["custNationality"] as String;
            this.ddli_maritalStatus.SelectedValue = Session["custMarital"] as String;
            String gender = Session["custGender"] as String;
            if ("Female".Equals(gender))
            {
                this.rbi_female.Checked = true;
            }
            else
            {

            } this.rbi_male.Checked = true;

            Object dob = Session["custDob"];
            if (dob != null)
            {
                DateTime dt = (DateTime)Session["custDob"];
                this.tbi_dob.Text = dt.ToString("dd/MM/yyyy");
            }

            this.tbi_tel.Text = Session["custTel"] as String;
            this.tbi_mobile.Text = Session["custMobile"] as String;
            this.tbi_email.Text = Session["custEmail"] as String;
            this.tbi_mailingAddress.Text = Session["custAddress"] as String;
            this.tbi_postal.Text = Session["custPostal"] as String;
            this.tbi_occupation.Text = Session["custOccupation"] as String;

        }
        
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid) return;
        var applicationEntity = new ApplyMainBLL
        {
            CustId =(String) Session["userId"],
            ApplicationSatus = "PENDING",
            AccountNo = "",
            ApplicationDate = DateTime.Now,
            StartDate = Util.StringToDate(tbStartDate.Text),
            EndDate = Util.StringToDate(tbStartDate.Text).Value.AddYears(1),
            PaymentWay = ddlPaymentMethod.SelectedValue,
            StaffId = null,
            SavingAccountNo =(String) Session["savingAccountNo"],
           
            ApplyProduct = "INSURANCE",
            ProductType = "A"
        };
        // applicationEntity.ApprovalDate=null;
        Console.WriteLine(applicationEntity.ToString());

        var insureds = new LinkedList<ApplyInsuredBLL>();

        if (!Util.IsInputBlank(tbi_firstName.Text))
        {
            var selfInsuranedEntity = new ApplyInsuredBLL
            {
                FirstName = tbi_firstName.Text,
                LastName = tbi_lastName.Text,
                Nric = tbi_nric.Text,
                Nationality = tbi_nationality.Text,
                IsMale = rbi_male.Checked,
            
                MaritalStatus = ddli_maritalStatus.Text,
                Dob = Util.StringToDate(tbi_dob.Text),
                Occupation = tbi_occupation.Text,
                Relationship = "SELF",
                Tel = tbi_tel.Text,
                Mobile = tbi_mobile.Text,
                Email = tbi_email.Text,
                MailingAddress = tbi_mailingAddress.Text,
                Postal = tbi_postal.Text,
                Employer = tbe_name.Text,
                CompanyAddress = tbe_address.Text,
                Business = tbe_business.Text,
                StartDate=Util.StringToDate(this.tbStartDate.Text),
                EndDate = Util.StringToDate(tbStartDate.Text).Value.AddYears(1),

                NatureOfIllness = (String)(GetDisease(tbi_firstName.Text + " " + tbi_lastName.Text)["NatureOfIllness"]),
                Hazardous = (String)(GetHazardous(tbi_firstName.Text + " " + tbi_lastName.Text)["Hazardous"]),
                TravelFrequency = (String)(GetTravelFrequency(tbi_firstName.Text + " " + tbi_lastName.Text)["TravelFrequency"]),
                DeclinedHistory = (String)(GetDeclinedHistory(tbi_firstName.Text + " " + tbi_lastName.Text)["DeclinedHistory"]),

            };

            if (this.rbi_gold.Checked)
            {
                selfInsuranedEntity.Plan = "Gold";
            }
            else if (this.rbi_silver.Checked)
            {
                selfInsuranedEntity.Plan = "Silver";
            }else
            {
                selfInsuranedEntity.Plan = "Bronze";
            }

            if (this.rbi_yes.Checked)
            {
                selfInsuranedEntity.LongStay = true;
            }
            else
            {
                selfInsuranedEntity.LongStay = false;
                
            }


            insureds.AddLast(selfInsuranedEntity);
        }

         if (!Util.IsInputBlank(tbs_fname.Text))
        {
            var spouseInsuranedEntity = new ApplyInsuredBLL
                {
                    FirstName = tbs_fname.Text,
                    LastName = tbs_lname.Text,
                    Nric = tbs_nric.Text,
                    Nationality = null,
                    IsMale = rbs_male.Checked,
                    LongStay = null,
                    MaritalStatus = "Married",
                    Dob = Util.StringToDate(tbs_dob.Text),
                    Occupation = tbs_occupation.Text,
                    Relationship = "SPOUSE",
                    Tel = null,
                    Mobile = null,
                    Email = null,
                    MailingAddress = null,
                    Postal = null,
                    Employer = null,
                    CompanyAddress = null,
                    Business = null,
                    StartDate = Util.StringToDate(this.tbStartDate.Text),
                    EndDate = Util.StringToDate(tbStartDate.Text).Value.AddYears(1),
                    NatureOfIllness = (String)(GetDisease(tbi_firstName.Text + " " + tbi_lastName.Text)["NatureOfIllness"]),
                    Hazardous = (String)(GetHazardous(tbi_firstName.Text + " " + tbi_lastName.Text)["Hazardous"]),
                    TravelFrequency = (String)(GetTravelFrequency(tbi_firstName.Text + " " + tbi_lastName.Text)["TravelFrequency"]),
                    DeclinedHistory = (String)(GetDeclinedHistory(tbi_firstName.Text + " " + tbi_lastName.Text)["DeclinedHistory"]),


                };

            if (this.rbs_gold.Checked)
            {
                spouseInsuranedEntity.Plan = "Gold";
            }
            else if (this.rbs_silver.Checked)
            {
                spouseInsuranedEntity.Plan = "Silver";
            }
            else
            {
                spouseInsuranedEntity.Plan = "Bronze";
            }
            insureds.AddLast(spouseInsuranedEntity);

        }

         if (!Util.IsInputBlank(tbc1_fname.Text))
         {
             var child1InsuranedEntity = new ApplyInsuredBLL
             {

                 FirstName = tbc1_fname.Text,
                 LastName = tbc1_lname.Text,
                 Nric = tbc1_nric.Text,
                 Nationality = null,
                 IsMale = rbc1_male.Checked,
                 LongStay = null,
                 MaritalStatus = "Single",
                 Dob = Util.StringToDate(tbc1_dob.Text),
                 Occupation = null,
                 Relationship = "CHILD",
                 Tel = null,
                 Mobile = null,
                 Email = null,
                 MailingAddress = null,
                 Postal = null,
                 Employer = null,
                 CompanyAddress = null,
                 Business = null,
                 StartDate = Util.StringToDate(this.tbStartDate.Text),
                 EndDate = Util.StringToDate(tbStartDate.Text).Value.AddYears(1),

                 NatureOfIllness = (String)(GetDisease(tbi_firstName.Text + " " + tbi_lastName.Text)["NatureOfIllness"]),
                 Hazardous = (String)(GetHazardous(tbi_firstName.Text + " " + tbi_lastName.Text)["Hazardous"]),
                 TravelFrequency = (String)(GetTravelFrequency(tbi_firstName.Text + " " + tbi_lastName.Text)["TravelFrequency"]),
                 DeclinedHistory = (String)(GetDeclinedHistory(tbi_firstName.Text + " " + tbi_lastName.Text)["DeclinedHistory"]),


             };
             if (this.rbc_gold.Checked)
             {
                 child1InsuranedEntity.Plan = "Gold";
             }
             else if (this.rbc_silver.Checked)
             {
                 child1InsuranedEntity.Plan = "Silver";
             }
             else
             {
                 child1InsuranedEntity.Plan = "Bronze";
             }
             insureds.AddLast(child1InsuranedEntity);

         }

         if (!Util.IsInputBlank(tbc2_fname.Text))
         {
             var child2InsuranedEntity = new ApplyInsuredBLL
             {

                 FirstName = tbc2_fname.Text,
                 LastName = tbc2_lname.Text,
                 Nric = tbc2_nric.Text,
                 Nationality = null,
                 IsMale = rbc2_male.Checked,
                 MaritalStatus = "Single",
                 LongStay = null,
                 Dob = Util.StringToDate(tbc2_dob.Text),
                 Occupation = null,
                 Relationship = "CHILD",
                 Tel = null,
                 Mobile = null,
                 Email = null,
                 MailingAddress = null,
                 Postal = null,
                 Employer = null,
                 CompanyAddress = null,
                 Business = null,
                 StartDate = Util.StringToDate(this.tbStartDate.Text),
                 EndDate = Util.StringToDate(tbStartDate.Text).Value.AddYears(1),

                 NatureOfIllness = (String)(GetDisease(tbi_firstName.Text + " " + tbi_lastName.Text)["NatureOfIllness"]),
                 Hazardous = (String)(GetHazardous(tbi_firstName.Text + " " + tbi_lastName.Text)["Hazardous"]),
                 TravelFrequency = (String)(GetTravelFrequency(tbi_firstName.Text + " " + tbi_lastName.Text)["TravelFrequency"]),
                 DeclinedHistory = (String)(GetDeclinedHistory(tbi_firstName.Text + " " + tbi_lastName.Text)["DeclinedHistory"]),


             };
             if (this.rbc_gold.Checked)
             {
                 child2InsuranedEntity.Plan = "Gold";
             }
             else if (this.rbc_silver.Checked)
             {
                 child2InsuranedEntity.Plan = "Silver";
             }
             else
             {
                 child2InsuranedEntity.Plan = "Bronze";
             }

             insureds.AddLast(child2InsuranedEntity);

         }

         if (!Util.IsInputBlank(tbc3_fname.Text))
         {
             var child3InsuranedEntity = new ApplyInsuredBLL
             {

                 FirstName = tbc3_fname.Text,
                 LastName = tbc3_lname.Text,
                 Nric = tbc3_nric.Text,
                 Nationality = null,
                 IsMale = rbc3_male.Checked,
                 LongStay = null,
                 MaritalStatus = "Single",
                 Dob = Util.StringToDate(tbc3_dob.Text),
                 Occupation = null,
                 Relationship = "CHILD",
                 Tel = null,
                 Mobile = null,
                 Email = null,
                 MailingAddress = null,
                 Postal = null,  
                 Employer = null,
                 CompanyAddress = null,
                 Business = null,
                 StartDate = Util.StringToDate(this.tbStartDate.Text),
                 EndDate = Util.StringToDate(tbStartDate.Text).Value.AddYears(1),

                 NatureOfIllness = (String)(GetDisease(tbi_firstName.Text + " " + tbi_lastName.Text)["NatureOfIllness"]),
                 Hazardous = (String)(GetHazardous(tbi_firstName.Text + " " + tbi_lastName.Text)["Hazardous"]),
                 TravelFrequency = (String)(GetTravelFrequency(tbi_firstName.Text + " " + tbi_lastName.Text)["TravelFrequency"]),
                 DeclinedHistory = (String)(GetDeclinedHistory(tbi_firstName.Text + " " + tbi_lastName.Text)["DeclinedHistory"]),


             };
             if (this.rbc_gold.Checked)
             {
                 child3InsuranedEntity.Plan = "Gold";
             }
             else if (this.rbc_silver.Checked)
             {
                 child3InsuranedEntity.Plan = "Silver";
             }
             else
             {
                 child3InsuranedEntity.Plan = "Bronze";
             }

             insureds.AddLast(child3InsuranedEntity);

         }

         applicationEntity.Insureds = insureds;

         // INSERT APPLICATION_INSURANCE AND APPLICATION_INSURANCE_INSURED TABLE
         applicationEntity.ApplyInsurance();
         this.Response.Redirect("./insurance_after_submit.aspx");

    }

    private Hashtable GetDisease(String fullName)
    {
        var hash = new Hashtable();
        if (!Util.IsInputBlank(tbq1_name1.Text))
        {
            if (tbq1_name1.Text == fullName)
            {
                hash.Add("NatureOfIllness", tbq1_disease1.Text);


            }
        }
        else if (!Util.IsInputBlank(tbq1_name2.Text))
        {
            if (tbq1_name2.Text == fullName)
            {
                hash.Add("NatureOfIllness", tbq1_disease2.Text);


            }
        }
        else if (!Util.IsInputBlank(tbq1_name3.Text))
        {
            if (tbq1_name3.Text == fullName)
            {
                hash.Add("NatureOfIllness", tbq1_disease3.Text);


            }
        }
        else if (!Util.IsInputBlank(tbq1_name4.Text))
        {
            if (tbq1_name4.Text == fullName)
            {

                hash.Add("NatureOfIllness", tbq1_disease4.Text);

            }
        }

        return hash;

    }

    private Hashtable GetHazardous(String fullName)
    {
        var hash = new Hashtable();
        if (!Util.IsInputBlank(tbq2_name1.Text))
        {
            if (tbq2_name1.Text == fullName)
            {
                hash.Add("Hazardous", tbq2_hazardous1.Text);


            }
        }
        else if (!Util.IsInputBlank(tbq2_name2.Text))
        {
            if (tbq2_name2.Text == fullName)
            {
                hash.Add("Hazardous", tbq2_hazardous2.Text);


            }
        }
        else if (!Util.IsInputBlank(tbq2_name3.Text))
        {
            if (tbq2_name3.Text == fullName)
            {
                hash.Add("Hazardous", tbq2_hazardous3.Text);


            }
        }
        else if (!Util.IsInputBlank(tbq2_name4.Text))
        {
            if (tbq2_name4.Text == fullName)
            {

                hash.Add("Hazardous", tbq2_hazardous4.Text);

            }
        }

        return hash;

    }

    private Hashtable GetTravelFrequency(String fullName)
    {
        var hash = new Hashtable();
        if (!Util.IsInputBlank(tbq3_name1.Text))
        {
            if (tbq3_name1.Text == fullName)
            {
                hash.Add("TravelFrequency", tbq3_travelFrequency1.Text);


            }
        }
        else if (!Util.IsInputBlank(tbq3_name2.Text))
        {
            if (tbq3_name2.Text == fullName)
            {
                hash.Add("TravelFrequency", tbq3_travelFrequency2.Text);


            }
        }
        else if (!Util.IsInputBlank(tbq3_name3.Text))
        {
            if (tbq3_name3.Text == fullName)
            {
                hash.Add("TravelFrequency", tbq3_travelFrequency3.Text);


            }
        }
        else if (!Util.IsInputBlank(tbq3_name4.Text))
        {
            if (tbq3_name4.Text == fullName)
            {

                hash.Add("TravelFrequency", tbq3_travelFrequency4.Text);

            }
        }

        return hash;

    }

    private Hashtable GetDeclinedHistory(String fullName)
    {
        var hash = new Hashtable();
        if (!Util.IsInputBlank(tbq4_name1.Text))
        {
            if (tbq4_name1.Text == fullName)
            {
                hash.Add("DeclinedHistory", tbq4_declinedHistory1.Text);


            }
        }
        else if (!Util.IsInputBlank(tbq4_name2.Text))
        {
            if (tbq4_name2.Text == fullName)
            {
                hash.Add("DeclinedHistory", tbq4_declinedHistory2.Text);


            }
        }
        else if (!Util.IsInputBlank(tbq4_name3.Text))
        {
            if (tbq4_name3.Text == fullName)
            {
                hash.Add("DeclinedHistory", tbq4_declinedHistory3.Text);


            }
        }
        else if (!Util.IsInputBlank(tbq4_name4.Text))
        {
            if (tbq4_name4.Text == fullName)
            {

                hash.Add("DeclinedHistory", tbq4_declinedHistory4.Text);

            }
        }

        return hash;

    }
    protected void cbAgree_CheckedChanged(object sender, EventArgs e)
    {
        if (this.cbAgree.Checked)
        {

            this.btnSubmit.Enabled = true;
        }
        else
        {
            this.btnSubmit.Enabled = false;

        }
    }



    protected void vd_family_ServerValidate(object source, ServerValidateEventArgs args)
    {
        // Check item 1
        string[] inputs1 = { this.tbs_fname.Text, this.tbs_lname.Text, this.tbs_nric.Text, this.tbs_dob.Text };
        checkValid(inputs1, args);

        // Check item 2
        string[] inputs2 = { this.tbc1_fname.Text, this.tbc1_lname.Text, this.tbc1_nric.Text, this.tbc1_dob.Text };
        checkValid(inputs2, args);

        // Check item 3
        string[] inputs3 = { this.tbc2_fname.Text, this.tbc2_lname.Text, this.tbc2_nric.Text, this.tbc2_dob.Text };
        checkValid(inputs3, args);

        // Check item 4
        string[] inputs4 = { this.tbc3_fname.Text, this.tbc3_lname.Text, this.tbc3_nric.Text, this.tbc3_dob.Text };
        checkValid(inputs4, args);

    }


    
    protected void vd_disease_ServerValidate(object source, ServerValidateEventArgs args)
    {
        // Check item 1
        string[] inputs1 = { this.tbq1_name1.Text, this.tbq1_disease1.Text };
        checkValid(inputs1, args);

        // Check item 2
        string[] inputs2 = { this.tbq1_name2.Text, this.tbq1_disease2.Text };
        checkValid(inputs2, args);

        // Check item 3
        string[] inputs3 = { this.tbq1_name3.Text, this.tbq1_disease3.Text };
        checkValid(inputs3, args);

        // Check item 4
        string[] inputs4 = { this.tbq1_name4.Text, this.tbq1_disease4.Text };
        checkValid(inputs4, args);
    }


    private void checkValid(string[] inputs, ServerValidateEventArgs args)
    {

        if (!Util.checkAllBlankInput(inputs))
        {
            if (Util.checkAllNotBlankInput(inputs))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
    }
    protected void vd_hazardous_ServerValidate(object source, ServerValidateEventArgs args)
    {
        // Check item 1
        string[] inputs1 = { this.tbq2_name1.Text, this.tbq2_hazardous1.Text };
        checkValid(inputs1, args);

        // Check item 2
        string[] inputs2 = { this.tbq2_name2.Text, this.tbq2_hazardous2.Text };
        checkValid(inputs2, args);

        // Check item 3
        string[] inputs3 = { this.tbq2_name3.Text, this.tbq2_hazardous3.Text };
        checkValid(inputs3, args);

        // Check item 4
        string[] inputs4 = { this.tbq2_name4.Text, this.tbq2_hazardous4.Text };
        checkValid(inputs4, args);
    }
    protected void vd_travelFrequency_ServerValidate(object source, ServerValidateEventArgs args)
    {

        // Check item 1
        string[] inputs1 = { this.tbq3_name1.Text, this.tbq3_travelFrequency1.Text };
        checkValid(inputs1, args);

        // Check item 2
        string[] inputs2 = { this.tbq3_name2.Text, this.tbq3_travelFrequency2.Text };
        checkValid(inputs2, args);

        // Check item 3
        string[] inputs3 = { this.tbq3_name3.Text, this.tbq3_travelFrequency3.Text };
        checkValid(inputs3, args);

        // Check item 4
        string[] inputs4 = { this.tbq3_name4.Text, this.tbq3_travelFrequency4.Text };
        checkValid(inputs4, args);
    }
    protected void vd_declinedHistory_ServerValidate(object source, ServerValidateEventArgs args)
    {
        // Check item 1
        string[] inputs1 = { this.tbq4_name1.Text, this.tbq4_declinedHistory1.Text };
        checkValid(inputs1, args);

        // Check item 2
        string[] inputs2 = { this.tbq4_name2.Text, this.tbq4_declinedHistory2.Text };
        checkValid(inputs2, args);

        // Check item 3
        string[] inputs3 = { this.tbq4_name3.Text, this.tbq4_declinedHistory3.Text };
        checkValid(inputs3, args);

        // Check item 4
        string[] inputs4 = { this.tbq4_name4.Text, this.tbq4_declinedHistory4.Text };
        checkValid(inputs4, args);
    }
}